package edu.whu.xterm.activity;

import java.util.Iterator;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import edu.whu.xterm.R;
import edu.whu.xterm.User;
import edu.whu.xterm.http.RequestBuilder;
import edu.whu.xterm.util.Constants;
import edu.whu.xterm.util.UrlBuilder;

public class UserDetailWforumActivity extends GDLoadingActivity {

	static final String TAG = "UserDetailWforumActivity";

	private String uid;

	private String url;

	private User user;

	private View userView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setActionBarContentView(R.layout.activity_user_detail);
		userView = findViewById(R.id.user_detail);
		initLoadingView(userView);
		uid = getIntent().getStringExtra(TopicAndReplyWforumActivity.KEY_USER_ID);
		url = UrlBuilder.displayUser(uid);
		user = new User();
		user.uid = uid;
		executeLoadTask();
	}

	@Override
	public void executeLoadTask() {
		new UserDetailAsyncTask().execute();
	}

	void setText(int resId, String text) {
		((TextView) findViewById(resId)).setText(text);
	}

	void setProgress(int resId, int progress, int max) {
		ProgressBar progressBar = ((ProgressBar) findViewById(resId));
		progressBar.setMax(max);
		progressBar.setProgress(progress);
	}

	void setLevel(int progressBarResId, int levelTextResId, int experience) {
		String levelText;
		int level;
		if (experience < Constants.FRESHMEN) {
			level = Constants.FRESHMEN;
			levelText = experience + "/" + Constants.FRESHMEN;
		} else if (experience < Constants.GENERAL) {
			level = Constants.GENERAL;
			levelText = experience + "/" + Constants.GENERAL;
		} else if (experience < Constants.INTERMEDIATE) {
			level = Constants.INTERMEDIATE;
			levelText = experience + "/" + Constants.INTERMEDIATE;
		} else if (experience < Constants.ADVANCED) {
			level = Constants.ADVANCED;
			levelText = experience + "/" + Constants.ADVANCED;
		} else if (experience < Constants.VETERAN) {
			level = Constants.VETERAN;
			levelText = experience + "/" + Constants.VETERAN;
		} else if (experience < Constants.ELDERS) {
			level = Constants.ELDERS;
			levelText = experience + "/" + Constants.ELDERS;
		} else if (experience < Constants.FOUNDING) {
			level = Constants.FOUNDING;
			levelText = experience + "/" + Constants.FOUNDING;
		} else if (experience < Constants.SUPERMAN) {
			level = Constants.SUPERMAN;
			levelText = experience + "/" + Constants.SUPERMAN;
		} else {
			level = experience;
			levelText = "" + experience;
		}
		setText(levelTextResId, levelText);
		setProgress(progressBarResId, experience, level);
	}

	private class UserDetailAsyncTask extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected Boolean doInBackground(Void... params) {
			try {
				long start = System.currentTimeMillis();
				Document document = RequestBuilder.getBuilder().pageRequest(url).call();
				Log.d(TAG, "page request time cost:" + (System.currentTimeMillis() - start));
				start = System.currentTimeMillis();
				Elements doc = document.select("table.TableBorder1 td.TableBody1");
				doc.remove(0);
				Iterator<Element> it = doc.iterator();
				while (it.hasNext()) {
					Element element = it.next();
					String label = element.text();
					if ("昵 称：".equals(label)) {
						user.nick = it.next().text();
					} else if ("性 别：".equals(label)) {
						user.gender = it.next().text();
					} else if ("星 座：".equals(label)) {
						user.astrology = it.next().text();
					} else if ("Ｑ Ｑ：".equals(label)) {
						user.qq = it.next().text();
					} else if ("论坛职务：".equals(label)) {
						user.clazz = it.next().text();
					} else if ("帖子总数：".equals(label)) {
						user.postsCount = it.next().text();
					} else if ("门 派：".equals(label)) {
						user.faction = it.next().text();
					} else if ("登录次数：".equals(label)) {
						user.loginCount = it.next().text();
					} else if ("经验值：".equals(label)) {
						user.experience = it.next().text();
					} else if ("经验等级：".equals(label)) {
						user.experienceLevel = it.next().text();
					} else if ("生命力：".equals(label)) {
						user.vitality = it.next().text();
					} else if ("上次登录：".equals(label)) {
						user.lastLoginTime = it.next().text();
					} else if ("最后来访IP：".equals(label)) {
						user.lastLoginIp = it.next().text();
					} else if (label.startsWith("目前")) {
						user.online = label;
					}
				}

				Log.d(TAG, "page process cost:" + (System.currentTimeMillis() - start));

			} catch (Exception e) {
				e.printStackTrace();
				return Boolean.FALSE;
			}
			return Boolean.TRUE;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if (result.booleanValue()) {

				setText(R.id.user_details_name, user.uid);
				setText(R.id.user_details_level, user.experienceLevel);
				setLevel(R.id.user_experience_progress, R.id.user_details_level_progress_text,
						Integer.valueOf(user.experience.trim()));
				setText(R.id.user_nick, user.nick);
				setText(R.id.user_gender, user.gender);
				setText(R.id.user_qq, user.qq);
				setText(R.id.user_astrology, user.astrology);
				setText(R.id.user_clazz, user.clazz);
				setText(R.id.user_faction, user.faction);
				setText(R.id.user_vitality, user.vitality);
				setText(R.id.user_posts_count, user.postsCount);
				setText(R.id.user_login_count, user.loginCount);
				setText(R.id.user_last_login, user.lastLoginTime);
				setText(R.id.user_last_login_ip, user.lastLoginIp);
				loadSuccessed();
			} else {
				loadFailed();
			}
		}
	}

}
